package com.baidu.lib_leancloud

import cn.leancloud.LCUser
import cn.leancloud.types.LCNull
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import org.greenrobot.eventbus.EventBus


class UserManager private constructor(){
    var currentUser = LCUser().apply {
        username = "user"
    }
    companion object{
        val instance = UserManager()
    }
    fun createUser(userName: String,userPassword: String,userMobilePhoneNumber: String ?= null, userEmail: String ?= null){

        // 创建实例
        val user = LCUser()


// 等同于 user.put("username", "Tom")
        user.username = userName
        user.password = userPassword


// 可选
        user.email = userEmail
        user.mobilePhoneNumber = userMobilePhoneNumber


// 设置其他属性的方法跟 LCObject 一样
        user.put("gender", "secret")

        user.signUpInBackground().subscribe(object : Observer<LCUser>{
            override fun onSubscribe(d: Disposable) {

            }

            override fun onError(e: Throwable) {
                //创建失败
            }

            override fun onComplete() {

            }

            override fun onNext(user: LCUser) {
                //创建成功
            }

        })
    }
    fun logByUsername(userName: String,userPassword: String){
        LCUser.logIn(userName, userPassword).subscribe(object : Observer<LCUser?> {
            override fun onSubscribe(disposable: Disposable) {}
            override fun onNext(user: LCUser) {
                // 登录成功
                EventBus.getDefault().post(UserChangedEvent(true))
                currentUser = user
            }

            override fun onError(throwable: Throwable) {
                // 登录失败（可能是密码错误）
            }

            override fun onComplete() {}
        })
    }
    fun logByuserMobilePhoneNumber(userMobilePhoneNumber: String,userPassword: String){
        LCUser.loginByMobilePhoneNumber("+8619201680101", "cat!@#123")
            .subscribe(object : Observer<LCUser?> {
                override fun onSubscribe(disposable: Disposable) {}
                override fun onNext(user: LCUser) {
                    // 登录成功
                    EventBus.getDefault().post(UserChangedEvent(true))
                    currentUser = user
                }

                override fun onError(throwable: Throwable) {
                    // 登录失败（可能是密码错误）
                }

                override fun onComplete() {}
            })
    }
    fun requestLoginSmsCode(userMobilePhoneNumber: String){
        LCUser.requestLoginSmsCodeInBackground(userMobilePhoneNumber)
            .subscribe(object : Observer<LCNull>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onError(e: Throwable) {
                    // 调用出错
                }

                override fun onComplete() {

                }

                override fun onNext(t: LCNull) {
                    // 成功调用
                }

            })
    }
    fun signUpOrLoginByMobilePhoneInBackground(userMobilePhoneNumber: String,smsCode: String){
        LCUser.signUpOrLoginByMobilePhoneInBackground(userMobilePhoneNumber, smsCode)
            .subscribe(object : Observer<LCUser?> {
                override fun onSubscribe(disposable: Disposable) {}
                override fun onNext(user: LCUser) {
                    // 登录成功
                    EventBus.getDefault().post(UserChangedEvent(true))
                    currentUser = user
                }

                override fun onError(throwable: Throwable) {
                    // 验证码不正确
                }

                override fun onComplete() {}
            })
    }

}